﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace HDJ.Framework.Core
{
    public static class LanguageManager
    {
        //本地化语言含有那几种文件名
        public const string LanguagesSettingFileName = "LanguageSettingData";

        public const string LanTableKey = "key";
        public const string LanTableValue = "value";

        public static CallBack<SystemLanguage> SwitchLanguageCallback;
        public static SystemLanguage currentLanguage = SystemLanguage.ChineseSimplified;


        private static LanguageSettingData settingData;
        private static Dictionary<string, string> languageDataDic = new Dictionary<string, string>();

        // Use this for initialization
        public static void Initialize(bool isUseSystemLanguage)
        {
            settingData = LoadLocalizedLanguageSettingData();

            SystemLanguage setLan = settingData.defaultLanguage;
            if (setLan == SystemLanguage.Unknown)
                return;
            if (isUseSystemLanguage)
            {
                SystemLanguage sysLan = Application.systemLanguage;
                if (settingData.gameExistLanguages.Contains(sysLan))
                {
                    setLan = sysLan;
                }
                else
                {
                    if (sysLan == SystemLanguage.Chinese)
                    {
                        SystemLanguage tempName = SystemLanguage.English;

                        if (settingData.gameExistLanguages.Contains(ChangeChineseLangusgeType2SystemLanguage(settingData.chineseLanguagePriorityLanguage)))
                            tempName = ChangeChineseLangusgeType2SystemLanguage(settingData.chineseLanguagePriorityLanguage);
                        else if (settingData.gameExistLanguages.Contains(ChangeChineseLangusgeType2SystemLanguage(ChineseLangusgeType.ChineseSimplified)))
                            tempName = ChangeChineseLangusgeType2SystemLanguage(ChineseLangusgeType.ChineseSimplified);
                        else if (settingData.gameExistLanguages.Contains(ChangeChineseLangusgeType2SystemLanguage(ChineseLangusgeType.ChineseTraditional)))
                            tempName = ChangeChineseLangusgeType2SystemLanguage(ChineseLangusgeType.ChineseTraditional);

                        setLan = tempName;
                    }
                }
            }
            SetUseLanguage(setLan);
        }

        private static SystemLanguage ChangeChineseLangusgeType2SystemLanguage(ChineseLangusgeType c)
        {
            return (SystemLanguage)Enum.Parse(typeof(SystemLanguage), c.ToString());
        }
        private static void SetUseLanguage(SystemLanguage setLan = SystemLanguage.Unknown)
        {
            if (setLan == SystemLanguage.Chinese)
                setLan = SystemLanguage.ChineseSimplified;
            if(setLan == SystemLanguage.Unknown)
            {
                Debug.LogError("SetUseLanguage :Unknown");
                return;
            }

            Clear();
            currentLanguage = setLan;
            
          
        }

        public static LanguageSettingData LoadLocalizedLanguageSettingData()
        {
            if (!ResourceManager.ExistAssets(LanguagesSettingFileName))
            {
                Debug.LogWarning("No Language Config:" + LanguagesSettingFileName);
                return new LanguageSettingData();
            }

            string text = ResourceManager.LoadText(LanguagesSettingFileName);
            LanguageSettingData settingData = JsonUtils.FromJson<LanguageSettingData>(text);
            if (settingData == null)
                settingData = new LanguageSettingData();

            return settingData;
        }
        public static void LoadLanguageData(SystemLanguage languageName, string fullKey)
        {
            int endIndex = fullKey.LastIndexOf('/');
            fullKey = fullKey.Remove(endIndex);

            string fileName = currentLanguage.ToString() + "_" + fullKey.Replace('/', '@');

            TableData tableData = TableManager.GetTableData(fileName);
            foreach (var line in tableData.datas.Values)
            {
                string key = fullKey + "/" + line[LanTableKey];
                string value = line[LanTableValue].ToString();
                languageDataDic.Add(key, value);
            }

        }
        public static void Clear()
        {
            languageDataDic.Clear();
        }
        public static void SwichLanguage(SystemLanguage languageName)
        {
            if (currentLanguage == languageName)
                return;
            if (!settingData.gameExistLanguages.Contains(languageName))
            {
                Debug.LogError("不支持的语言：" + languageName);
                return;
            }
            Clear();
            SetUseLanguage(languageName);
            if (SwitchLanguageCallback != null)
                SwitchLanguageCallback(languageName);
        }
        public static SystemLanguage[] GetSupportLanguageName()
        {
            return settingData.gameExistLanguages.ToArray();
        }
        public static string GetText(string fullKey, params object[] paras)
        {
            string value = "";
            if (languageDataDic.ContainsKey(fullKey))
                value = languageDataDic[fullKey];
            else
            {
                LoadLanguageData(currentLanguage, fullKey);
            }
            if (languageDataDic.ContainsKey(fullKey))
                value = languageDataDic[fullKey];
            else
            {
                Debug.LogError("Key no find! key:" + fullKey);

                return null;
            }

            if (paras == null || paras.Length == 0)
                return value;
            else
            {
                string text = value;
                for (int i = 0; i < paras.Length; i++)
                {
                    string temp = "{" + i + "}";
                    if (paras[i] == null)
                        continue;

                    if (text.Contains(temp))
                        text = text.Replace(temp, paras[i].ToString());
                }
                return text;
            }
        }
    }
}
